/**
 * https://leetcode.cn/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/
 */
function kthLargest(root: TreeNode | null, k: number): number {
  let ans = 0,
    over = false;

  const preOrder = (node: TreeNode | null) => {
    if (over || node === null) return 0;
    preOrder(node.right);

    k--;
    if (k === 0) {
      over = true;
      ans = node.val;
      return;
    }
    preOrder(node.left);
  };

  preOrder(root);

  return ans;
}
